package com.team.controller;

import com.github.pagehelper.PageInfo;

import com.team.pojo.AppInfo;
import com.team.pojo.AppInfoCondition;
import com.team.pojo.DevUser;
import com.team.service.AppInfoService;
import com.team.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class AppInfoController {
@Autowired
 private AppInfoService appInfoService;
@RequestMapping("/searchApp")
 public String searchApp(AppInfoCondition appInfoCondition, Model model, HttpSession session){
 //从session中获取当前用户信息
 DevUser devUser=(DevUser)session.getAttribute("devUser");
 //设置用户编号到实体
 appInfoCondition.setUserid(devUser.getId());

 //调用业务查询
 PageInfo<AppInfo> pageInfo=appInfoService.getAppInfoCondition(appInfoCondition);
 //将查询的条件和分页信息填充到页面（回显)
 model.addAttribute("condition",appInfoCondition);
 model.addAttribute("pageInfo",pageInfo);

 //返回视图
 return "developer/appinfolist";
}
@RequestMapping("/addAppInfo")
 public String addAppInfo(@RequestParam(value ="a_logoPicPath",required = false)CommonsMultipartFile logoFile,
                          Model model,HttpServletRequest request,AppInfo appInfo){
/* try {
  //获取上传文件的位置
  String path=request.getSession().getServletContext().getRealPath("upload/logo");
  //唯一的文件名称
  String filename=file.getOriginalFilename();
  String expName=filename.substring(filename.lastIndexOf("."));
  String saveFileName=System.currentTimeMillis()+expName;
  File saveFile=new File(path+"/"+saveFileName);
  file.transferTo(saveFile);

 } catch (Exception e) {
  e.printStackTrace();
 }*/
 //文件上传，获取保存的位置
 try {
  String rpath="/upload/logo";
  String savePath=request.getSession().getServletContext().getRealPath("upload/logo");
  //上传
  String fileName=FileUploadUtil.uploadFile(savePath,logoFile);
  if (fileName!=null){
   //将信息添加到数据库
   //文件信息存至数据库，文件名+文件路径
   appInfo.setLogopicpath(rpath+"/"+fileName);
   appInfo.setCreationdate(new Date());//设置添加日期
   //区分用户
   HttpSession session=request.getSession();
   DevUser devUser=(DevUser) session.getAttribute("devUser");
   appInfo.setDevid(devUser.getId());
   appInfoService.addAppInfo(appInfo);
  }else {
   model.addAttribute("errorInfo","文件上传失败!!!");
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 return "developer/appinfoadd";
}
//获取当前APP信息
@RequestMapping("/editAppInfo")
 public String editAppInfo(Long id,Model model){
 //调用业务
 AppInfo appInfo=appInfoService.getAppInfo(id);
 //将信息填充到页面
 model.addAttribute("appInfo",appInfo);
 //返回页面
 return "developer/appinfomodify";
}

@RequestMapping("/updateAppInfo")
public String updateAppInfo(AppInfo appInfo,HttpSession session,String oldPath,HttpServletRequest request,
 @RequestParam(value = "a_logoPicPath",required = false)CommonsMultipartFile file){
 //1.处理文件上传
 try {
 if(!file.isEmpty()) {
  //如果选择图片则上传新图
  String path = "upload/logo";
  //相对路径转换成绝对路径
  String absolutePath = request.getSession().getServletContext().getRealPath(path);
  String fileName = FileUploadUtil.uploadFile(absolutePath, file);
  if (fileName != null) {
   //上传成功
   //给app信息实体设置图片的路径   修改图片路径
   appInfo.setLogopicpath(path + "/" + fileName);

   //删除原图
   File oldFile = new File(request.getSession().getServletContext().getRealPath(oldPath));
   if (oldFile.exists()) {
    oldFile.delete();
   }
   //保存信息到数据库
   appInfoService.updateAppInfo(appInfo);
  } else {
   session.setAttribute("errorInfo", "上传文件失败啦，添加失败!");
  }
 }
 else{
  //保存信息到数据库  不修改图片路径
  appInfoService.updateAppInfo(appInfo);
 }
 session.setAttribute("errorInfo","修改信息成功！");
}catch (Exception e){
  session.setAttribute("errorInfo","修改信息失败!");
 }
 return "redirect:editAppInfo?id="+appInfo.getId();
}
@RequestMapping("/deleteAppInfo")
 public String deleteAppInfo(Long appId,String path,HttpServletRequest request){
 try {
  appInfoService.deleteAppInfo(appId);
  //删除app信息图片
  String delPath=request.getSession().getServletContext().getRealPath(path);
  File delFile=new File(delPath);
  if (delFile.exists())
   delFile.delete();
 } catch (Exception e) {
  e.printStackTrace();
 }
 return "redirect:searchApp";
}
@RequestMapping("/upDownApp")//编写控制器请求实现上架下架
@ResponseBody
 public Map<String,Integer> upDownApp(Long id,Long state){
  Map<String,Integer> map=new HashMap<>();
  //调用业务
 int temp=appInfoService.updateStatus(id, state);
 map.put("result",temp);
 return map;
}
}
